package com.xxh.dasongapp.controller;

import com.xxh.dasongapp.common.Result;
import com.xxh.dasongapp.utils.FileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping(value = "/upload")
@Api(tags = "文件模块接口")
@Slf4j
public class FileController {

    @ApiOperation(value = "上传图片")
    @PostMapping(value = "/img")
    public Result<?> imageupload(@RequestParam(value = "imgFile",required = true) MultipartFile imgFile) throws IOException {
        String realPath = System.getProperty("user.dir") + "/upload/img/";
        String imgName = FileUtils.saveFile(imgFile, realPath);
        return Result.success(imgName);
    }

    @ApiOperation(value = "删除图片")
    @DeleteMapping(value = "/imgdelete/{fileName}")
    public Result<?> filedelete(@PathVariable  String fileName){
        String realPath = System.getProperty("user.dir") + "/upload/img/";
        if (FileUtils.removeFile(fileName,realPath)){
            return Result.success("删除成功");
        }else {
            return Result.success("删除失败");
        }
    }


}
